/**
 * May 21, 2014 11:03:58 AM
 */
package elitech.vietnam.copyweatherfashion.fragments.settings;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.google.gson.Gson;

import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.prefs.PrefsDefinition;

/**
 * @author Cong
 *
 */
public class NotificationsFragment extends Fragment implements OnCheckedChangeListener {

	MainActivity mActivity;
	
	CheckBox mCbEnable, mCbFriendPost, mCbMyStyle, mCbReply, mCbFriendAddMe, mCbSound;
	public NotificationsFragment() {
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.settings_notifications, container, false);
		
		mCbEnable = (CheckBox) view.findViewById(R.id.stnoti_cbEnable);
		mCbFriendPost = (CheckBox) view.findViewById(R.id.stnoti_cbFriendPost);
		mCbMyStyle = (CheckBox) view.findViewById(R.id.stnoti_cbMystyleComment);
		mCbReply = (CheckBox) view.findViewById(R.id.stnoti_cbReplyComment);
		mCbFriendAddMe = (CheckBox) view.findViewById(R.id.stnoti_cbFriendAddMe);
		mCbSound = (CheckBox) view.findViewById(R.id.stnoti_cbEnableSound);
		
		mCbEnable.setChecked(mActivity.mOptions.mNotiEnable);
		mCbFriendPost.setChecked(mActivity.mOptions.mNotiFriendPost);
		mCbMyStyle.setChecked(mActivity.mOptions.mNotiComment);
		mCbReply.setChecked(mActivity.mOptions.mNotiRepComment);
		mCbFriendAddMe.setChecked(mActivity.mOptions.mNotiAddFriend);
		mCbSound.setChecked(mActivity.mOptions.mNotiSound);
		
		mCbEnable.setOnCheckedChangeListener(this);
		mCbFriendPost.setOnCheckedChangeListener(this);
		mCbMyStyle.setOnCheckedChangeListener(this);
		mCbReply.setOnCheckedChangeListener(this);
		mCbFriendAddMe.setOnCheckedChangeListener(this);
		mCbSound.setOnCheckedChangeListener(this);
		
		return view;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) { 
		case R.id.stnoti_cbEnable: 
			mActivity.mOptions.mNotiEnable = isChecked;
			mCbFriendPost.setEnabled(isChecked);
			mCbMyStyle.setEnabled(isChecked);
			mCbReply.setEnabled(isChecked);
			mCbFriendAddMe.setEnabled(isChecked);
//			if (isChecked) {
//				mCbFriendPost.setChecked(isChecked); mActivity.mOptions.mNotiFriendPost = isChecked;
//				mCbMyStyle.setChecked(isChecked); mActivity.mOptions.mNotiComment = isChecked;
//				mCbReply.setChecked(isChecked); mActivity.mOptions.mNotiRepComment= isChecked;
//				mCbFriendAddMe.setChecked(isChecked); mActivity.mOptions.mNotiAddFriend = isChecked;
//			}
			break;
		case R.id.stnoti_cbEnableSound:
			mActivity.mOptions.mNotiSound = isChecked;			
			break;
		case R.id.stnoti_cbFriendPost:
			mActivity.mOptions.mNotiFriendPost = isChecked;
			break;
		case R.id.stnoti_cbMystyleComment:
			mActivity.mOptions.mNotiComment = isChecked;
			break;
		case R.id.stnoti_cbReplyComment:
			mActivity.mOptions.mNotiRepComment = isChecked;
			break;
		case R.id.stnoti_cbFriendAddMe:
			mActivity.mOptions.mNotiAddFriend = isChecked;
			break;
		default:
			break;
		}
		mActivity.mPrefs.edit().putString(PrefsDefinition.OPTION, new Gson().toJson(mActivity.mOptions)).commit();
	}
}
